#!/bin/csh
##
## Purpose: get daily GPS, GLONASS and mixed RINEX broadcast ephemris files from CDDIS
##
## Originally written by Feng Zhou on 03/11/2015 @ GFZ
##
## Email: zhouforme@163.com
##
## Section 1.1, GPS/Galileo technologies, GFZ German Research Centre for Geosciences
#
##===============================BEGIN PROGRAM=====================================##
if ($#argv < 3) then
more << EOF

   Usage $0 <path> <yyyy> <doy> [ndays]

   EXAMPLES: $0 ~/datafolder 2017 001
             $0 ~/datafolder 2017 001 10

EOF
   exit
endif

set navdir = $1
@ yyyy  = $2
set yy = `echo $yyyy | awk '{printf("%2.2d",$1-int($1/100)*100)}'`
@ doy = $3
@ count = 1
if ($#argv >= 4) then
   @ count = $4
endif

echo "Download to:" $navdir

#set navdir = /Users/zhoufeng/Documents/GNSS_download/nav
if (! -d $navdir) /bin/mkdir -p $navdir
cd $navdir

echo " "
echo " Downloading IGS NAV files from ftp://cddis.gsfc.nasa.gov ..."
echo " "

while ($count)
   set cdoy = `echo $doy | awk '{printf("%3.3d\n",$1)}'`
   echo " Year:" $yyyy " DOY:" $cdoy
   
   wget -qr -nH --cut-dirs=7 ftp://cddis.gsfc.nasa.gov/pub/gps/data/daily/$yyyy/$cdoy/$yy"n"/brdc$cdoy"0."$yy"n.Z"
   wget -qr -nH --cut-dirs=7 ftp://cddis.gsfc.nasa.gov/pub/gps/data/daily/$yyyy/$cdoy/$yy"g"/brdc$cdoy"0."$yy"g.Z"
   wget -qr -nH --cut-dirs=9 ftp://cddis.gsfc.nasa.gov/pub/gps/data/campaign/mgex/daily/rinex3/$yyyy/brdm/brdm$cdoy"0."$yy"p.Z"
   
   echo "Downloading completed"
   
   sh_uncompress brdc$cdoy"0."$yy"g.Z" brdc$cdoy"0."$yy"n.Z" brdm$cdoy"0."$yy"p.Z"

   @ count --
   @ doy ++
end

#
##================================END PROGRAM======================================##
